package com.oopnet.template.test.proxy;

import java.lang.reflect.Method;
import java.rmi.RemoteException;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import com.oopnet.template.test.proxy.impl.MyRemoteImpl;

public class CGLibProxy implements MethodInterceptor{

	private Object target;
	
	public Object getInstance(Object object){
		this.target = object;
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(this.target.getClass());
		enhancer.setCallback(this);
		return enhancer.create();
	}
	
	@Override
	public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy proxy) throws Throwable {
		System.out.println("Before");
		proxy.invokeSuper(arg0, arg2);
		System.out.println("After");
		
		return null;
	}
	public static void main(String[] args) {
		try {
			
			CGLibProxy cgLibProxy = new CGLibProxy();
			MyRemoteImpl impl =(MyRemoteImpl) cgLibProxy.getInstance(new MyRemoteImpl());
			impl.sayHello();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
